#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstring>
using namespace std;
const int N = 20;
int a[N];
int t[N];
int n;
int calc(int x, int y)
{
    int ret = 0;
    for (int i = 0; i < n; i++)
    {
        if (((x >> i) & 1) == 1 && ((y >> i) & 1) == 0)
            return -1;
        if (((x >> i) & 1) == 0 && ((y >> i) & 1) == 1)
            ret++;
    }
    return ret;
}
int solve()
{
    int ret = 0x3f3f3f3f;
    for (int i = 0; i < (1 << n); i++)
    {
        int st = i;
        int cnt = 0;
        int flag = 1;
        memcpy(t, a, sizeof(a));
        for (int j = 1; j <= n; j++)
        {
            int c = calc(t[j], st);
            if (c == -1)
            {
                flag = 0;
                break;
            }
            cnt += c;
            t[j] = st;
            st = t[j - 1] ^ (t[j] << 1) ^ (t[j] >> 1);
            st &= ((1 << n) - 1);
        }
        if (flag)
        {
            ret = min(ret, cnt);
        }
    }
    if (ret == 0x3f3f3f3f)
    {
        return -1;
    }
    return ret;
}
int main()
{
    int T;
    cin >> T;
    for (int i = 1; i <= T; i++)
    {
        memset(a, 0, sizeof(a));
        cin >> n;
        for (int j = 1; j <= n; j++)
        {
            for (int k = 0; k < n; k++)
            {
                int x;
                cin >> x;
                if (x) a[j] |= 1 << k;
            }
        }
        printf("Case %d: %d\n", i, solve());
    }
    return 0;
}